package central;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import GUI.Ventana;
import control_monitor.Monitor;

public class pieza_B implements Runnable{

	private Monitor monitor; 
	int socketmaq3;
	int socketmaq4;
	int socketR2;
	Ventana gui;
	
	public pieza_B(Monitor monit,int sokr2,int sokm3,int sokm4, Ventana _gui){
		monitor=monit;
			socketmaq3=sokm3;
			socketmaq4=sokm4;
			socketR2=sokr2;
			gui = _gui;	
	}

	public void run(){
		
			//ejecutar el camino B
		    monitor.ejecutar_transicion(7);
		    gui.SetRobot(2,4);
		    gui.setnumI(2, -1);
		    
		    
					
			DataOutputStream sale_flujoR2 = null;
			Socket socketA = null;
			try {
				socketA=new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketA.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de I2 a M3");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			DataInputStream entra_flujoR2 = null;
			String mensaje = null;
			try {
				entra_flujoR2 = new DataInputStream(socketA.getInputStream());
				mensaje=entra_flujoR2.readUTF();//robot confirma movimiento
				System.out.println(mensaje);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socketA.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			gui.SetRobot(2, 1);
			monitor.ejecutar_transicion(8);
			gui.setMaquina(3, 1, 2);
			
			
			
			DataOutputStream sale_flujoM3;
			Socket socketB = null;
			try {
				socketB=new Socket("localhost",socketmaq3);
				sale_flujoM3 = new DataOutputStream(socketB.getOutputStream());
				sale_flujoM3.writeUTF("procesar pieza en m3");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			DataInputStream entra_flujoM3;
			String mensajemaq = null;
			try {
				entra_flujoM3 = new DataInputStream(socketB.getInputStream());
				mensajemaq=entra_flujoM3.readUTF();//maquina confirma procesamiento
				System.out.println(mensajemaq);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				socketB.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//entra_flujoM3.close();

			
			monitor.ejecutar_transicion(9);
			gui.SetRobot(2, 1);
			gui.setMaquina(3, -1, 2);
			
			
					
			Socket socketC = null;
			try {
				socketC=new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketC.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de m3 a m4");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

			String mensajeR2 = null;
			try {
				entra_flujoR2 = new DataInputStream(socketC.getInputStream());
				mensajeR2=entra_flujoR2.readUTF();
				System.out.println(mensajeR2);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//robot confirma movimiento
			
			try {
				socketC.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			gui.SetRobot(2, 7);
			monitor.ejecutar_transicion(10);	
			gui.setMaquina(4, 1, 2);
			
			
			DataOutputStream sale_flujoM4;
			Socket socketD = null;
			try {
				socketD = new Socket("localhost",socketmaq4);
				sale_flujoM4 = new DataOutputStream(socketD.getOutputStream());
				sale_flujoM4.writeUTF("procesar pieza en m4");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//sale_flujoM4.close();
			
			DataInputStream entra_flujoM4;
			String mensajemaq4 = null;
			try {
				entra_flujoM4 = new DataInputStream(socketD.getInputStream());
				mensajemaq4=entra_flujoM4.readUTF();//maquina confirma procesamiento
				System.out.println(mensajemaq4);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socketD.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//entra_flujoM4.close();
			
			
			monitor.ejecutar_transicion(11);
			gui.SetRobot(2, 7);
			gui.setMaquina(4, -1, 2);
			
				
			Socket socketE = null;
			try {
				socketE=new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketE.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de m4 a O2");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//sale_flujoR2.close();

			String mensajeR2_bis = null;
			try {
				entra_flujoR2 = new DataInputStream(socketE.getInputStream());
				mensajeR2_bis=entra_flujoR2.readUTF();
				System.out.println(mensajeR2_bis);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//robot confirma movimiento

			//entra_flujoR2.close();
			try {
				socketE.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//monitor.devolver robot
			
			gui.SetRobot(2, 0);
			monitor.ejecutar_transicion(12);	
			gui.setnumO(2, 1);
			

			System.out.println("ya termino el camino B");
	}
}
